package com.yunjijia.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunjijia.mapper.BuiltMapper;
import com.yunjijia.pojo.Built;
import com.yunjijia.pojo.BuiltExample;
import com.yunjijia.service.BuiltService;
import com.yunjijia.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BuiltServiceImpl implements BuiltService {

    @Resource
    private BuiltMapper builtMapper;


    @Override
    public void save(Built built) {
        builtMapper.insertSelective(built);
    }

    @Override
    public void del(long id) {
        //将状态改为下架
        Built built = builtMapper.selectByPrimaryKey(id);
        built.setState("2");
        builtMapper.updateByPrimaryKeySelective(built);
    }

    @Override
    public void dels(long[] ids) {
        for (long id : ids) {
            Built built = builtMapper.selectByPrimaryKey(id);
            built.setState("2");
            builtMapper.updateByPrimaryKeySelective(built);
        }
    }

    @Override
    public void update(Built built) {
        builtMapper.updateByPrimaryKeySelective(built);
    }

    @Override
    public Built selectById(long id) {
        return builtMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Built> selectAll() {
        return builtMapper.selectByExample(null);
    }

    @Override
    public PageResult page(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Built> list = builtMapper.selectByExample(null);
        PageInfo<Built> pageInfo = new PageInfo<>(list);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public PageResult requirementSearch(Integer pageNum, Integer pageSize, Built built) {
        PageHelper.startPage(pageNum, pageSize);
        BuiltExample builtExample = new BuiltExample();
        BuiltExample.Criteria criteria = builtExample.createCriteria();
        if (null != built.getName() && !"".equals(built.getName().trim())) {
            criteria.andNameLike("%" + built.getName().trim() + "%");
        }
        List<Built> list = builtMapper.selectByExample(builtExample);
        PageInfo<Built> pageInfo = new PageInfo<>(list);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }
}
